package com.example.administrator.familfinalcialmanagement.ui.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.example.administrator.familfinalcialmanagement.R;
import com.example.administrator.familfinalcialmanagement.bean.GoodsIdBean;
import com.example.administrator.familfinalcialmanagement.bean.LoginBean;
import com.example.administrator.familfinalcialmanagement.bean.StroeBean;
import com.example.administrator.familfinalcialmanagement.bean.StroeIdBean;
import com.example.administrator.familfinalcialmanagement.databinding.ActivityToastBinding;
import com.example.administrator.familfinalcialmanagement.ui.BaseActivity;
import com.example.administrator.familfinalcialmanagement.util.ApiHTTP;
import com.example.administrator.familfinalcialmanagement.util.GlideUtil;
import com.example.administrator.familfinalcialmanagement.util.GsonUtil;
import com.example.administrator.familfinalcialmanagement.util.LogUtil;
import com.example.administrator.familfinalcialmanagement.util.SpUtils;
import com.example.administrator.familfinalcialmanagement.util.ToastUtil;
import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class ToastActivity extends BaseActivity {

    ActivityToastBinding binding;
    String name, factoryName, img, count, id;
    StroeBean.GoodsListBean goodsBean;
    List<GoodsIdBean> listGoodsBean = new ArrayList<>();
    double price;
    LoginBean bean = (LoginBean) SpUtils.getBean("module_loginBean");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        binding = DataBindingUtil.setContentView( this, R.layout.activity_toast );
        binding.setActivity( this );
        init();
    }

    private void init() {
        setBack();
        goodsBean = (StroeBean.GoodsListBean) getIntent().getSerializableExtra("module_bean");
        LogUtil.e("goodsBean-->"+ goodsBean.getName());
        factoryName = getIntent().getStringExtra( "module_factoryName" );
        name = getIntent().getStringExtra( "module_name" );
        img = getIntent().getStringExtra( "module_img" );
        count = getIntent().getStringExtra( "module_count" );
        id = getIntent().getStringExtra( "module_id" );
        price = Double.valueOf( getIntent().getStringExtra( "module_price" ) );
        setTitleText( factoryName );

        if (!TextUtils.isEmpty( img )) {
            GlideUtil.setUriMethod( this, img, binding.ivModuleStore );
        }

        binding.tvModuleName.setText( name );
        binding.tvModuleFactoryName.setText( factoryName );
        binding.tvModuleCount.setText( count );
        binding.tvModulePrice.setText( price + "" );
        binding.tvModuleAllPrice.setText( price + "" );

        binding.tvModuleDelete.setOnClickListener( this );
        binding.tvModuleAdd.setOnClickListener( this );
        binding.textViewMain.setOnClickListener( this );
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvModuleDelete:
                int num = Integer.valueOf( binding.tvModuleNum.getText().toString().trim() );
                if (num > 1) {
                    binding.tvModuleNum.setText( (num - 1) + "" );
                    double money = ( num - 1 ) * price;
                    binding.tvModuleAllPrice.setText( money + "" );
                }else{
                    ToastUtil.showToast( "商品数量最少为一个" );
                }
                break;
            case R.id.tvModuleAdd:
                int numS = Integer.valueOf( binding.tvModuleNum.getText().toString().trim() );
                binding.tvModuleNum.setText( (numS + 1) + "" );
                double moneyS = ( numS + 1 ) * price;
                binding.tvModuleAllPrice.setText( moneyS + "" );

                break;
            case R.id.textViewMain:
                String address = binding.etModuleAddress.getText().toString().trim();
                if (TextUtils.isEmpty( address )) {
                    ToastUtil.showToast( "请输入收获地址" );
                    return;
                }

                String count = binding.tvModuleNum.getText().toString().trim();

                double money1 = Integer.valueOf( count ) * price;

                LogUtil.e("商品数量-->"+ count);
                //根据后台传参调整提交的参数
                goodsBean.setCount(Integer.valueOf(count));
                listGoodsBean.add(new GoodsIdBean(goodsBean.getName(),count,goodsBean.getId() + "",
                        goodsBean.getPrice() + "",bean.getData().getId() + ""));
                ApiHTTP.getInstance().createOrder( address,
                        "1", money1 + "",
                        bean.getData().getId() + "",
                        count, GsonUtil.BeanToJson( listGoodsBean ).toString(), new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                 LogUtil.e( "联网成功"+ s );
                                 ToastUtil.showToast("购买成功");
                                 finish();
                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                ToastUtil.showToast( "联网失败" );
                            }
                        } );

                break;
            default:
        }
    }


}
